package com.ruoyi.common.utils.hikvisionutils;
/**
 * ClassName: HttpClientUtils <br/>
 * Description: <br/>
 * date: 2020/12/22 15:32<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import com.hikvision.artemis.sdk.Client;
import com.hikvision.artemis.sdk.Request;
import com.hikvision.artemis.sdk.Response;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.hikvision.artemis.sdk.constant.Constants;
import com.hikvision.artemis.sdk.enums.Method;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: HiKVision
 * @description:  在官方示例的基础上修改
 * @author: hesion
 * @create: 2020-12-22 15:32
 **/
public class HikVisionHttpUtils {
    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
    private static final String SUCC_PRE = "2";
    private static final String REDIRECT_PRE = "3";

    public static Response doPostArtemis(Map<String, String> path, String body, Map<String, String> querys, String accept, String contentType) {
        String httpSchema = (String) path.keySet().toArray()[0];
        if(httpSchema != null && !StringUtils.isEmpty(httpSchema)){
            // 存放头部信息
            Response response = null;
            try {
                Map<String,String> headers = new HashMap();
                if(StringUtils.isNotBlank(accept)){
                    headers.put("Accept",accept);
                }else {
                    headers.put("Accept","*/*");
                }
                if (StringUtils.isNotBlank(contentType)) {
                    headers.put("Content-Type", contentType);
                } else {
                    headers.put("Content-Type", "application/text;charset=UTF-8");
                }

                Request request = new Request(Method.POST_STRING, httpSchema + ArtemisConfig.host, (String)path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
                request.setHeaders(headers);
                request.setQuerys(querys);
                request.setStringBody(body);
                request.setTimeout(5000);
                response = Client.execute(request);
                return response;
            }catch (Exception e){
                logger.error("the Artemis PostString Request is failed[doPostStringArtemis]", e);
            }
            return response;
        }else {
            throw new RuntimeException("http和https参数错误httpSchema: " + httpSchema);
        }
    }
}
